package info.gridworld.actor;

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

public class CrazyReproductionCrab extends Critter {
	private int sex;
//	private Location loc;

	public CrazyReproductionCrab(int sex) {
		this.sex = sex;
		colorChange();
	}

	public ArrayList<Actor> getActors() {
		ArrayList<Actor> actor;
		actor = getGrid().getNeighbors(getLocation());
		return actor;
	}

	@Override
	public void makeMove(Location loc) {
		this.colorChange();
		super.makeMove(loc);
	}

	public void processActors(ArrayList<Actor> actors) {

		for (Actor a : actors) {
			if (a instanceof CrazyReproductionCrab) {
				if (((CrazyReproductionCrab) a).sex != this.sex) {

					int newSex = (int) (Math.random() * 2);

					ArrayList<Location> emptyLocs = this.getEmptyLocations();
					if (!emptyLocs.isEmpty()) {
						CrazyReproductionCrab sibilyCrab = new CrazyReproductionCrab(
								newSex);
						int sibilyChoose = (int) (Math.random() * emptyLocs
								.size());
						Location newLoc = emptyLocs.get(sibilyChoose
								% emptyLocs.size());
						sibilyCrab.putSelfInGrid(getGrid(), newLoc);
					}

				}
			}
		}
	}

	private ArrayList<Location> getEmptyLocations() {
		ArrayList<Location> emptyLocation = new ArrayList<Location>();

		int row = getGrid().getNumRows();
		int col = getGrid().getNumCols();
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (getGrid().get(new Location(i, j)) == null) {
					emptyLocation.add(new Location(i, j));
				}
			}
		}
		return emptyLocation;
	}

	private void colorChange() {
		int red = (int) (Math.random() * 255);
		int green = (int) (Math.random() * 255);
		int blue = (int) (Math.random() * 255);
		this.setColor(new Color(red, green, blue));

	}
}
